package ai.people.platform.system.service.impl;

import ai.people.netmon.framework.domain.system.entity.NmSystemRoleUser;
import ai.people.netmon.framework.exception.enums.CommonEnum;
import ai.people.netmon.framework.exception.enums.SystemExceptionEnum;
import ai.people.netmon.framework.utils.AssertUtils;
import ai.people.platform.system.mapper.SysRoleUserMapper;
import ai.people.platform.system.service.SysRoleUserService;
import ai.people.platform.system.service.SystemRoleService;
import ai.people.platform.system.service.SystemUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 角色用户 服务实现类
 * </p>
 *
 * @author zhenglin
 * @since 2022-03-22
 */
@Service
@RequiredArgsConstructor
public class SysRoleUserServiceImpl extends ServiceImpl<SysRoleUserMapper, NmSystemRoleUser> implements SysRoleUserService {

    private final SystemUserService systemUserServiceImpl;

    private final SystemRoleService systemRoleServiceImpl;


    /**
     * 保存角色用户
     *
     * @param roleUser 用户角色
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRoleUser(NmSystemRoleUser roleUser) {
        systemUserServiceImpl.checkUserExists(roleUser.getUserId());
        systemRoleServiceImpl.checkRoleExists(roleUser.getRoleId());
        this.deleteRoleUser(roleUser.getUserId());
        boolean save = this.save(roleUser);
        AssertUtils.isTrue(save, CommonEnum.FAIL);
    }

    /**
     * 删除用户角色
     *
     * @param userId 用户id
     */
    @Override
    public void deleteRoleUser(Long userId) {
        List<NmSystemRoleUser> roleUserByUserId = this.selectRoleUserByUserId(userId);
        if (roleUserByUserId.isEmpty()) {
           return;
        }
        LambdaQueryWrapper<NmSystemRoleUser> lambdaQuery = Wrappers.lambdaQuery(NmSystemRoleUser.class);
        lambdaQuery.eq(NmSystemRoleUser::getUserId, userId);
        boolean removeResult = this.remove(lambdaQuery);
        AssertUtils.isTrue(removeResult, SystemExceptionEnum.USER_ROLE_UPDATE_FAILED);
    }


    /**
     * 通过角色id选择角色用户
     *
     * @param roleId 角色id
     * @return {@link List}<{@link NmSystemRoleUser}>
     */
    @Override
    public List<NmSystemRoleUser> selectRoleUserByRoleId(Long roleId) {
        LambdaQueryWrapper<NmSystemRoleUser> lambdaQuery = Wrappers.lambdaQuery(NmSystemRoleUser.class);
        lambdaQuery.eq(NmSystemRoleUser::getRoleId, roleId);
        return this.list(lambdaQuery);
    }

    /**
     * 通过用户id查询用户角色信息
     *
     * @param userId 用户id
     * @return {@link List}<{@link NmSystemRoleUser}>
     */
    @Override
    public List<NmSystemRoleUser> selectRoleUserByUserId(Long userId) {
        LambdaQueryWrapper<NmSystemRoleUser> lambdaQuery = Wrappers.lambdaQuery(NmSystemRoleUser.class);
        lambdaQuery.eq(NmSystemRoleUser::getUserId, userId);
        return this.list(lambdaQuery);
    }


}
